@page "/link_track/link/fss"
@page "/link_track/link/fss/{pageIndex:int}"
@using FOPS.Abstract.MetaInfo.Entity
@using FOPS.Abstract.MetaInfo.Server
@using FS.Core
@using Microsoft.AspNetCore.Components
@using FS.Core.LinkTrack
@using FS.Extends
@using FS.LinkTrack
@using FS.LinkTrack.Dal
@inject IIocManager _iocManager;
@inject NavigationManager nav;
@inject IJSRuntime js;

<div class="layui-card">
    <div class="layui-card-header" id="anchor-play-header">
        应用
        <select style="display:inline;width: 200px;" @onchange="GroupChange">
            <option value="">全部</option>
            @foreach (var projectVO in _lstAppId)
            {
                <option value="@projectVO.AppId">@projectVO.AppId</option>
            }
        </select>
        <div class="layui-input-inline w80">
            <input type="text" class="layui-input" placeholder="任务组名称" style="height: 30px;width:200px;" @bind="_taskGroupName">
        </div>
        
        <div class="layui-input-inline w120">
            <input type="text" class="layui-input" placeholder="任务组ID" style="height: 30px;width:80px;" @bind="_taskGroupId">
        </div>
        <div class="layui-input-inline w100">
            <input type="text" class="layui-input" placeholder="客户端IP" style="height: 30px;width:120px;" @bind="_searchIp">
        </div>
        <div class="layui-input-inline w100">
            <input type="text" class="layui-input" placeholder="耗时" style="height: 30px;width:60px;" @bind="searchUseTs">
        </div>
        <select style="display:inline;width: 150px;" @onchange="SortChange">
            <option value="0">按最新时间</option>
            <option value="60">1小时耗时最高</option>
            <option value="30">30分钟耗时最高</option>
            <option value="10">10分钟耗时最高</option>
            <option value="5">5分钟耗时最高</option>
            <option value="1">1分钟耗时最高</option>
        </select>
        <div class="layui-input-inline">
            <button class="layui-btn layui-btn-sm" lay-submit="" lay-filter="orders-market-search-submit" @onclick="GetList"><i class="am-icon-search"></i> 搜索</button>
        </div>
    </div>
    <div class="layui-card-body">
        <div class="layui-form layui-border-box layui-table-view" lay-filter="LAY-table-1" style=" ">
            <div class="layui-table-box">
                <div class="layui-table-header">
                    <table cellspacing="0" cellpadding="0" border="0" class="layui-table" lay-skin="auto" lay-size="sm">
                        <thead>
                        <tr>
                            <th data-field="ID">
                                <div class="layui-table-cell laytable-cell-14-0-0">
                                    <span>AppId</span>
                                </div>
                            </th>
                            <th data-field="Type">
                                <div class="layui-table-cell laytable-cell-14-0-1">
                                    <span>ContextId</span>
                                </div>
                            </th>
                            <th data-field="Type">
                                <div class="layui-table-cell laytable-cell-14-0-2">
                                    <span>请求时间</span>
                                </div>
                            </th>
                            <th data-field="Type">
                                <div class="layui-table-cell laytable-cell-14-0-3">
                                    <span>耗时</span>
                                </div>
                            </th>
                            <th data-field="Type">
                                <div class="layui-table-cell laytable-cell-14-0-4">
                                    <span>任务组名称</span>
                                </div>
                            </th>
                            <th data-field="Type">
                                <div class="layui-table-cell laytable-cell-14-0-5">
                                    <span>任务组ID</span>
                                </div>
                            </th>
                            <th data-field="Type">
                                <div class="layui-table-cell laytable-cell-14-0-6">
                                    <span>客户端IP</span>
                                </div>
                            </th>
                            <th data-field="Type">
                                <div class="layui-table-cell laytable-cell-14-0-7">
                                    <span>数据</span>
                                </div>
                            </th>
                        </tr>
                        </thead>
                    </table>
                </div>
                <div class="layui-table-body layui-table-main">
                    <table cellspacing="0" cellpadding="0" border="0" class="layui-table" lay-skin="auto" lay-size="sm">
                        <tbody>
                        @for (var index = 0; index < _lst.List.Count; index++)
                        {
                            var info = _lst.List[index];
                            <tr data-index="@index">
                                <td>
                                    <div class="layui-table-cell laytable-cell-14-0-0"> @info.AppId</div>
                                </td>
                                <td data-field="Title">
                                    <div class="layui-table-cell laytable-cell-14-0-1">
                                        <a href="/link_track/link/info/@(info.ContextId)">@info.ContextId</a>
                                    </div>
                                </td>
                                <td data-field="Title">
                                    <div class="layui-table-cell laytable-cell-14-0-2">
                                        @info.StartTs.ToTimestamps().ToString("yyyy-MM-dd HH:mm:ss")
                                    </div>
                                </td>
                                <td data-field="Title">
                                    <div class="layui-table-cell laytable-cell-14-0-3">
                                        @if (info.UseTs >= 100)
                                        {
                                            <strong class="layui-badge layui-bg-red">@(info.UseTs) ms</strong>
                                        }
                                        else if (info.UseTs >= 50)
                                        {
                                            <strong class="layui-badge layui-bg-blue">@(info.UseTs) ms</strong>
                                        }
                                        else
                                        {
                                            <span>@(info.UseTs) ms</span>
                                        }
                                    </div>
                                </td>
                                <td data-field="Title">
                                    <div class="layui-table-left laytable-cell-14-0-4">
                                        @info.Method 
                                    </div>
                                </td>
                                <td data-field="Title">
                                    <div class="layui-table-cell laytable-cell-14-0-5">
                                        <a href="/link_track/link/info/@(info.ContextId)">
                                            @info.Path
                                        </a>
                                    </div>
                                </td>
                                <td data-field="Title">
                                    <div class="layui-table-cell laytable-cell-14-0-6">
                                        @info.RequestIp
                                    </div>
                                </td>
                                <td data-field="Title">
                                    <div class="layui-table-cell laytable-cell-14-0-7">
                                        @info.RequestBody
                                    </div>
                                </td>
                            </tr>
                        }
                        </tbody>
                    </table>
                </div>
            </div>
            <PageSplit PageIndex="@PageIndex" PageSize="@_pageSize" TotalCount="@_lst.RecordCount" PageUrl="/link_track/link/fss/{index}"></PageSplit>
        </div>
    </div>
</div>
<style>
    .laytable-cell-14-0-0{ width: 130px; }
    .laytable-cell-14-0-1{ width: 160px; }
    .laytable-cell-14-0-2{ width: 150px; }
    .laytable-cell-14-0-3{ width: 100px; }
    .laytable-cell-14-0-4{ width: 200px; }
    .laytable-cell-14-0-5{ width: 100px; }
    .laytable-cell-14-0-6{ width: 180px; }
    .laytable-cell-14-0-7{ width: 500px; }
    select { width: 150px; }
</style>

@code{

    private int _lastIndex;
    private int _pageSize = 20;
    [Parameter]
    public int PageIndex { get; set; }
    private bool _isFirst;
    private PageList<LinkTrackContextPO> _lst = new();
    private List<ProjectVO> _lstAppId = new();
    private string _curAppId = "";
    private string _curSort = "0";

    private string _taskGroupId;
    private string _searchIp;
    private string _taskGroupName;
    private string searchUseTs;

    protected override async Task OnParametersSetAsync()
    {
        if (_isFirst && _lastIndex == PageIndex) return;
        _isFirst = true;

        if (PageIndex < 1) PageIndex = 1;
        _lastIndex = PageIndex;

        _lstAppId = await _iocManager.Resolve<IProjectService>().ToAppListAsync() ?? new();
        GetList();
    }

    private void GroupChange(ChangeEventArgs args)
    {
        _curAppId = args.Value.ToString();
        GetList();
    }
    private void SortChange(ChangeEventArgs args)
    {
        _curSort = args.Value.ToString();
        GetList();
    }

    public void GetList()
    {
        _taskGroupId = _taskGroupId?.Trim();
        _searchIp = _searchIp?.Trim();
        var indexSet = LinkTrackEsContext.Data.LinkTrackContext
                                         .Where(t => t.LinkType == EumLinkType.Fss)
                                         .Where(t => t.ParentAppId == "")
                                         .Where(t => t.AppId == _curAppId)
                                         .Where(t => t.RequestIp == _searchIp)
                                         .Where(t => t.UseTs >= searchUseTs.ConvertType(0))
                                         .Where(t => t.Method == _taskGroupName)
                                         .Where(o => o.Path.Contains(_taskGroupId));


        if (_curSort == "0") indexSet.Desc(d => d.StartTs);
        else
        {
            indexSet.Where(o => o.StartTs >= DateTime.Now.AddMinutes(-_curSort.ConvertType(0)).ToTimestamps())
                    .Desc(d => d.UseTs);
        }

        _lst = indexSet.ToPageList(_pageSize, PageIndex);
    }
}